package com.robcity.platform.impl;

import com.robcity.platform.api.RobotEventInfoService;
import com.robcity.platform.convert.RobotEventInfoConvert;
import com.robcity.platform.entities.RobotEventInfo;
import com.robcity.platform.enums.RobNameEnum;
import com.robcity.platform.enums.RobotEventHandelStatus;
import com.robcity.platform.enums.RobotEventTypeEnum;
import com.robcity.platform.repo.RobotEventInfoRepo;
import com.robcity.platform.util.WGS84ToGCJ02;
import com.robcity.platform.vo.RobotEventInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zhangsong
 * @Date 2024/8/10
 **/
@Service
public class RobotEventInfoServiceImpl implements RobotEventInfoService {
    @Resource
    RobotEventInfoRepo robotEventInfoRepo;

    @Override
    public List<RobotEventInfoVO> getRobotEventInfo() {
        List<RobotEventInfo> robotEventInfoList = robotEventInfoRepo.selectRobotEventInfo();
        List<RobotEventInfoVO> robotEventInfoVOList = null;
        if(!CollectionUtils.isEmpty(robotEventInfoList)){
            robotEventInfoVOList = robotEventInfoList.stream().map(RobotEventInfoConvert::RobotEventInfo2RobotEventInfoVO).collect(Collectors.toList());
            robotEventInfoVOList.forEach(robotEventInfoVO -> {
                if(StringUtils.isNoneBlank(robotEventInfoVO.getEventType())){
                    RobotEventTypeEnum robotEventTypeEnum = RobotEventTypeEnum.getByCode(robotEventInfoVO.getEventType());
                    robotEventInfoVO.setEventTypeName(robotEventTypeEnum != null?robotEventTypeEnum.getDesc():null);
                }
                if(StringUtils.isNoneBlank(robotEventInfoVO.getHandelStatus())){
                    RobotEventHandelStatus robotEventHandelStatus = RobotEventHandelStatus.getByCode(robotEventInfoVO.getHandelStatus());
                    robotEventInfoVO.setHandelStatusName(robotEventHandelStatus != null?robotEventHandelStatus.getDesc():null);
                }
                if (StringUtils.isNotBlank(robotEventInfoVO.getRobId())) {
                    RobNameEnum robNameEnum = RobNameEnum.getByCode(robotEventInfoVO.getRobId());
                    robotEventInfoVO.setRobName(robNameEnum != null ? robNameEnum.getName() : null);
                }
                if (robotEventInfoVO.getOccurLat() != null && robotEventInfoVO.getOccurLon() != null) {
                    robotEventInfoVO.setOccurLonOrg(robotEventInfoVO.getOccurLon());
                    robotEventInfoVO.setOccurLatOrg(robotEventInfoVO.getOccurLat());
                    //返回顺序 lon lat
                    double[] gcj02 = WGS84ToGCJ02.wgs84ToGcj02(robotEventInfoVO.getOccurLat().doubleValue(), robotEventInfoVO.getOccurLon().doubleValue());
                    robotEventInfoVO.setGcj02(gcj02);
                    robotEventInfoVO.setOccurLon(BigDecimal.valueOf(gcj02[0]));
                    robotEventInfoVO.setOccurLat(BigDecimal.valueOf(gcj02[1]));
                    robotEventInfoVO.setGcj02Sub(String.format("%.3f,%.3f", gcj02[0], gcj02[1]));
                }
            });
        }

        return robotEventInfoVOList;
    }
}
